package com.admin.education.controllers.api.v1;


import com.admin.education.common.response.IResponseMessage;
import com.admin.education.common.response.ResponseMessageWithData;
import com.admin.education.common.response.Result;
import com.admin.education.common.exceptions.BadRequestException;
import com.admin.education.common.jwt.JwtAuthorize;
import com.admin.education.common.jwt.JwtUtils;
import com.admin.education.dataTransferObject.PageRequest;
import com.admin.education.dataTransferObject.PageResponse;
import com.admin.education.dataTransferObject.ServicesExcuteResult;
import com.admin.education.dataTransferObject.templates.TemplatesTransfer;
import com.admin.education.dataTransferObject.userAnswerSheet.Request.CreateUserAnswersSheetRequest;
import com.admin.education.dataTransferObject.userAnswerSheet.Request.SubmitPaperRequest;
import com.admin.education.dataTransferObject.userAnswerSheet.UserAnswerSheetTransfer;
import com.admin.education.dataTransferObject.userInformation.UserInformationTransfer;
import com.admin.education.services.Interfaces.ITemplateService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.concurrent.Callable;

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/v1/templates")
public class TemplateController {
    @Autowired
    private ITemplateService templateService;

    public TemplateController(){

    }
    @GetMapping("")
    public Callable<IResponseMessage> queryTemplates(@Valid PageRequest pageRequest){
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult<PageResponse<TemplatesTransfer>> result = templateService.queryTemplates(pageRequest);
                if(result.isSuccess()){
                    responseMessage = new ResponseMessageWithData<PageResponse<TemplatesTransfer>>(Result.Success,result.getData());
                }
                else{
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }

    @GetMapping("{id}")
    //@JwtAuthorize
    public Callable<IResponseMessage> queryTemplates(@PathVariable Long id){
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult<TemplatesTransfer> result = templateService.queryTemplateById(id);
                if(result.isSuccess()){
                    responseMessage = new ResponseMessageWithData<TemplatesTransfer>(Result.Success,result.getData());
                }
                else{
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
    @PostMapping("{id}/userAnswerSheets")
    @JwtAuthorize
    public Callable<IResponseMessage> createAnswerSheets(@PathVariable Long id, @RequestBody SubmitPaperRequest submitPaperRequest, HttpServletRequest request){
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;
                String auth =JwtUtils.getClaims(request,"auth").toString();

                if(null==auth)
                    throw new BadRequestException("请先登录");

                UserInformationTransfer userInformationTransfer = JSONObject.parseObject(auth,UserInformationTransfer.class);

                ServicesExcuteResult<UserAnswerSheetTransfer> result = templateService.createUserAnswerSheet(id,userInformationTransfer.getId(),userInformationTransfer.getRoles().getId(),submitPaperRequest);

                if(result.isSuccess()){
                    responseMessage = new ResponseMessageWithData<UserAnswerSheetTransfer>(Result.Success,result.getData());
                }
                else{
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
    @GetMapping("{id}/userAnswerSheets")
    @JwtAuthorize
    public Callable<IResponseMessage> queryAnswerSheets(@PathVariable Long id,HttpServletRequest request){
        return  new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;
                String auth =JwtUtils.getClaims(request,"auth").toString();

                if(null==auth)
                    throw new BadRequestException("请先登录");

                UserInformationTransfer userInformationTransfer = JSONObject.parseObject(auth,UserInformationTransfer.class);

                ServicesExcuteResult<List<UserAnswerSheetTransfer>> result = templateService.queryUserAnswerSheetByTemplateId(id,userInformationTransfer.getId());

                if(result.isSuccess()){
                    responseMessage = new ResponseMessageWithData<List<UserAnswerSheetTransfer>>(Result.Success,result.getData());
                }
                else{
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }

}
